//
//  Insurances.m
//  car
//
//  Created by chenxu on 3/31/15.
//  Copyright (c) 2015 chenxu. All rights reserved.
//

#import "Insurances.h"

@interface Insurances(){
    NSArray *names,*specialnames;
    NSArray *thirdlist;
}
@property BOOL firstload;

@end


@implementation Insurances

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        _firstload = true;
        thirdlist = @[@"5",@"10",@"20",@"30",@"50",@"100"];
        names = @[@"交通事故责任强制险",
                  @"第三者责任险",
                  @"车辆损失险",
                  @"全车盗抢险",
                  @"玻璃单独损失险",
                  @"自燃损失险",
                  @"发动机特别损失险",
                  @"车上人员责任险-司机",
                  @"车上人员责任险-乘客(4座)",
                  @"车身划痕险",
                  @"不计免赔特约险",
                  ];
        specialnames = @[
                         @"2.第三者责任险",
                         @"3.车辆损失险",
                         @"4.全车盗抢险",
                         @"7.发动机特别损失险",
                         @"8.车上人员责任险-司机",
                         @"9.车上人员责任险-乘客(4座)",
                         @"10.车身划痕险",
                        ];
    }
    return self;
}

- (instancetype)initWithDriver:(NSString*)str
{
    self = [super init];
    if (self) {
        _firstload = true;
        thirdlist = @[@"5",@"10",@"20",@"30",@"50",@"100"];
        names = @[@"交通事故责任强制险",
                  @"第三者责任险",
                  @"车辆损失险",
                  @"全车盗抢险",
                  @"玻璃单独损失险",
                  @"自燃损失险",
                  @"发动机特别损失险",
                  @"车上人员责任险-司机",
                  @"车上人员责任险-乘客(4座)",
                  @"车身划痕险",
                  @"不计免赔特约险",
                  ];
        specialnames = @[
                         @"2.第三者责任险",
                         @"3.车辆损失险",
                         @"4.全车盗抢险",
                         @"7.发动机特别损失险",
                         @"8.车上人员责任险-司机",
                         @"9.车上人员责任险-乘客(4座)",
                         @"10.车身划痕险",
                         ];
        [self loadInfo:str];
    }
    return self;
}

- (void)loadInfo:(NSString*)driver{
    NSString* loginstatus = [[NSString alloc]initWithFormat:@"%@_%@",driver,@"firstload"];
    id temp =  [[NSUserDefaults standardUserDefaults]objectForKey:loginstatus];
    
    //第一次登陆，先看是否存过firstload键，空说明第一次登陆
    if (temp == nil && _firstload == true) {
        _firstload = false;
        id drivers = [NSObject loadPlist:@"drivers"];//读磁盘的默认
        id driverinfo = [drivers objectForKey:driver];
        _selected = [[NSMutableArray alloc]initWithArray:[driverinfo objectForKey:@"diy"]];
        _inferred = [self getinferred:_selected];
        _third = @"20";
        _driver = driver;
    }
    //不空说明存过，若是false说明不是第一次登陆
    else if(temp != nil && [temp boolValue] == false){
        
        for (NSString *propertyname in self.propertyDictionary) {
            NSString* key = [[NSString alloc]initWithFormat:@"%@_%@",driver,propertyname];
            id data =  [[NSUserDefaults standardUserDefaults] objectForKey:key];
            if ([propertyname isEqualToString:@"selected"]) {       //读数组默认是array而不是mutablearray
                _selected = [[NSMutableArray alloc]initWithArray:(NSArray*)data];
            }
            else if ([propertyname isEqualToString:@"inferred"]){   //读数组默认是array而不是mutablearray
                _inferred = [[NSMutableArray alloc]initWithArray:(NSArray*)data];
            }
            else{
                [self setValue:data forKey:propertyname];
            }
        }
    }
    

    
}


- (void)saveInfo{
    for (id propertyname in self.propertyDictionary) {
        id tosave = [[self objectDictionary]objectForKey:propertyname];
        NSString* key = [[NSString alloc]initWithFormat:@"%@_%@",_driver,propertyname];
        [[NSUserDefaults standardUserDefaults] setObject:tosave forKey:key];
    }
}


//自动生成inferred数组，不包含“不计免赔”
- (NSMutableArray*)getinferred:(NSMutableArray*)selected{
 //   NSMutableArray* result = [[NSMutableArray alloc]initWithArray:selected];
    NSMutableArray* titles = [[NSMutableArray alloc]init];
    if (selected) {
        for (NSString* content in selected) {
            if ([content isEqualToString:@"1"]
                ||[content isEqualToString:@"5"]
                ||[content isEqualToString:@"6"]
                ||[content isEqualToString:@"11"]
                ) {
            }
            else{
                
                NSString* title = [names objectAtIndex:[content intValue]-1];
                title = [NSString stringWithFormat:@"%@.%@",content,title];
                [titles addObject:title];
            }
        }
        //排序
        NSArray* temp = [titles sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            //因为原来是string，默认排字典序，所以转成int再排序
            if ([obj1 integerValue] > [obj2 integerValue]) {
                return NSOrderedDescending;
            }
            if ([obj1 integerValue] < [obj2 integerValue]) {
                return NSOrderedAscending;
            }
            return NSOrderedSame;
        }];
        titles = [[NSMutableArray alloc]initWithArray:temp];
    }
    return titles;
}

- (NSString*)getInsuranceName:(NSInteger)index{
    if(index>=0 && index < [names count])
    {
        return [names objectAtIndex:  index ];
    }
    else{
        return @"没有这个保险名";
    }
}

- (NSString*)getSpecialName:(NSInteger)index{
    if(index>=0 && index < [specialnames count])
    {
        return [specialnames objectAtIndex:  index ];
    }
    else{
        return @"没有这个保险名";
    }
}

/*!
 *  @brief  选中某个保险
 *  @param index 某个保险的编号
 *  @return 成功、失败
 */
- (BOOL)selectAt:(NSInteger)index{
    if (index >=1 && index <=11) {
        NSString* str = [[NSString alloc]initWithFormat:@"%ld",(long)index];
        //正选、反选
        if (![_selected containsObject:str]) {
            [_selected addObject:str];
        }
        else{
            [_selected removeObject:str];
        }
        
        
        //对selected排序
        NSArray* temp = [_selected sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            //因为原来是string，默认排字典序，所以转成int再排序
            if ([obj1 integerValue] > [obj2 integerValue]) {
                return NSOrderedDescending;
            }
            if ([obj1 integerValue] < [obj2 integerValue]) {
                return NSOrderedAscending;
            }
            return NSOrderedSame;
        }];
        _selected = [[NSMutableArray alloc]initWithArray:temp];
        //更新inferred
        _inferred = [self getinferred:_selected];
        
        //保存到磁盘
        [self saveInfo];
        
        return true;
    }
    else{
        return false;
    }
}

- (NSArray*)getThirdlist{
    return thirdlist;
}

//加上第11个保险的数组
- (NSArray*)getlist{
    NSMutableArray* result = [[NSMutableArray alloc]initWithArray:_selected];
    if (![result containsObject:@"11"]) {
        [result addObject:@"11"];
    }
    return (NSArray*)result;
}

//生成json请求
- (NSString*)toJsonString{
    NSMutableString* ret = [[NSMutableString alloc]init];
    [ret appendString:@"["];
    
    for (NSString* index in [self getlist]) {
        NSString* xianming ;
        
        if ([index intValue] == 2) {//第三者责任险要加上价格
            xianming = [[NSString alloc]initWithFormat:@"%@.%@(%@万)",index,[names objectAtIndex:[index intValue]-1],_third];
        }
        else if ([index intValue] == 11){
            break;
        }
        else{
            xianming = [[NSString alloc]initWithFormat:@"%@.%@",index,[names objectAtIndex:[index intValue]-1 ]];
        }
        NSDictionary* unit = @{
                               @"xianming" : xianming,
                               @"price"    : @"0.0"
                               };
        NSString* json = [unit JSONString];
        
        [ret appendString:json];
        [ret appendString:@","];

    }
    
    //生成不计免赔的保险列表
    for (int i = 0; i < [_inferred count]; i++) {
        NSString* title = [_inferred objectAtIndex:i];
        NSString* xianming = [[NSString alloc]initWithFormat:@"不计免赔-%@",title];
        NSDictionary* unit = @{
                               @"xianming" : xianming,
                               @"price"    : @"0.0"
                               };
        NSString* json = [unit JSONString];
        [ret appendString:json];
        //最后一个元素不加逗号
        if ( i != [_inferred count]) {
            [ret appendString:@","];
        }
    }
    
    [ret appendString:@"]"];
 
    return ret;
}
@end
